package com.zzw.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.zzw.adapter.QuestionAdapter;
import com.zzw.util.BaseActivity;
import com.zzw.util.QuestionDao;

import java.util.List;
import java.util.Map;

public class QuestionActivity extends BaseActivity {

    // 显示list使用
    private List<Map<String,Object>> values;
    private ListView list;
    private QuestionAdapter adapter;

    // 分页使用
    private int pagesize = 15;
    private int pagenum = 1;
    private int recordCount;
    private int pageCount;

    private int first;
    private int visible;
    private int total;

    private String keyword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);
        super.init("全部问题", 1);

        list = (ListView) findViewById(R.id.list);
        keyword = getIntent().getStringExtra("keyword");
        if(keyword == null){
            keyword = "";
        }
        // 分页查询
        values = QuestionDao.loadDataPage(pagenum,pagesize,keyword);
        recordCount = QuestionDao.getCount(keyword);
        pageCount = (recordCount - 1)/pagesize + 1;

        // 加入footerView
        final TextView footerView = new TextView(QuestionActivity.this);
        footerView.setText("正在加载数据，请稍后。。。");
        footerView.setTextColor(Color.BLACK);
        footerView.setTextSize(14);
        list.addFooterView(footerView);

        adapter = new QuestionAdapter(QuestionActivity.this,values);
        list.setAdapter(adapter);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String, Object> map = values.get(position);
                LinearLayout linear = (LinearLayout) view;
                if (linear.getChildCount() == 1) {
                    TextView answerView = new TextView(QuestionActivity.this);
                    answerView.setText(map.get("answer").toString());
                    answerView.setTextSize(14);
                    answerView.setTextColor(Color.RED);
                    linear.addView(answerView);
                    map.put("showFlag",true);
                } else {
                    linear.removeViewAt(1);
                    map.put("showFlag",false);
                }
            }
        });

        list.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if(scrollState == SCROLL_STATE_IDLE && first+visible == total && total != 0){
                    if(pagenum<pageCount) {
                        pagenum++;
                        values.addAll(QuestionDao.loadDataPage(pagenum, pagesize, keyword));
                        adapter.notifyDataSetChanged();
                    }
                }else{
                    if(list.getFooterViewsCount()>0){
                        list.removeFooterView(footerView);
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                first = firstVisibleItem;
                visible = visibleItemCount;
                total = totalItemCount;
            }
        });
    }
}
